//
//  CZPictureWallViewController.m
//  i0S007通用项目
//
//  Created by Apple on 16/2/17.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "CZPictureWallViewController.h"
#import "CZTopItem.h"
#import "CZCategoryViewController.h"

@interface CZPictureWallViewController () <CZTopItemDelegate>

@property (nonatomic, strong) CZTopItem *categoryTopItem;
@end

@implementation CZPictureWallViewController

static NSString * const reuseIdentifier = @"Cell";

- (instancetype)init {
    if (self = [super initWithCollectionViewLayout:[[UICollectionViewFlowLayout alloc] init]]) {
        
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupNavLeft];
    [self setupNavRight];
    
    // 添加通知的监听
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(categoryChange:) name:CZCategoryDidChangeNofitication object:nil];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - 通知监听方法
- (void)categoryChange:(NSNotification *)notification {
    NSString *categoryName = notification.userInfo[@"CZCategoryeNameKey"];
    NSString *subcategoryName = notification.userInfo[@"CZSubCategoryeNameKey"];
    NSString *imageName = notification.userInfo[@"CZCategoryImageNameKey"];
    NSString *highlightedImageName = notification.userInfo[@"CZCategoryHighlightedImageNameKey"];
    
    // 设置topItem的内容
    self.categoryTopItem.title = categoryName;
    self.categoryTopItem.subTitle = subcategoryName;
    
    self.categoryTopItem.icon = imageName;
    self.categoryTopItem.hIcon = highlightedImageName;
}

/**
 *  设置导航栏左边
 */
- (void)setupNavLeft {
    self.categoryTopItem = [CZTopItem topItemWithIcon:@"icon_category_-1"  hightedIcon:@"icon_category_highlighted_-1" title:@"全部分类" subTitle:nil];
    self.categoryTopItem.frame = CGRectMake(0, 0, 100, 40);
    
    self.categoryTopItem.delegate = self;
    
    // 包装成UIBarBttonItem
    UIBarButtonItem *categoryButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.categoryTopItem];
    
    self.navigationItem.leftBarButtonItem = categoryButtonItem;
}

/**
 *  设置导航栏右边
 */
- (void)setupNavRight {
    // 地图
    UIButton *mapButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [mapButton addTarget:self action:@selector(mapButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    [mapButton setImage:[UIImage imageNamed:@"icon_map"] forState:UIControlStateNormal];
    [mapButton setImage:[UIImage imageNamed:@"icon_map_highlighted"] forState:UIControlStateHighlighted];
    [mapButton sizeToFit];
    
    // 包装 UIBarButtonItem
    UIBarButtonItem *mapItem = [[UIBarButtonItem alloc] initWithCustomView:mapButton];
    self.navigationItem.rightBarButtonItems = @[mapItem];
    
    // 搜索
    UIButton *searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [searchButton setImage:[UIImage imageNamed:@"icon_search"] forState:UIControlStateNormal];
    [searchButton setImage:[UIImage imageNamed:@"icon_search_highlighted"] forState:UIControlStateHighlighted];
    searchButton.frame = CGRectMake(0, 0, 60, 30);
    
    // 包装 UIBarButtonItem
    UIBarButtonItem *searchItem = [[UIBarButtonItem alloc] initWithCustomView:searchButton];
    
//    UIBarButtonItem *flexbleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    self.navigationItem.rightBarButtonItems = @[mapItem, searchItem];
}

#pragma mark - 点击事件
- (void)mapButtonClick {
    self.categoryTopItem.title = @"测试标题";
    self.categoryTopItem.subTitle = @"测试子标题";
}

#pragma mark - 代理方法
- (void)topItemClick:(CZTopItem *)topItem {
    CZCategoryViewController *categoryVC = [[CZCategoryViewController alloc] init];
    
    categoryVC.modalPresentationStyle = UIModalPresentationPopover;
    categoryVC.popoverPresentationController.barButtonItem = self.navigationItem.leftBarButtonItem;
    categoryVC.preferredContentSize = CGSizeMake(400, 400);
    
    [self presentViewController:categoryVC animated:YES completion:nil];
}
@end
